<?php

declare(strict_types=1);

namespace App\WorkDepartment\Logic;

use Hyperf\Di\Annotation\Inject;
use App\WorkEmployee\Service\WorkEmployeeService;
use App\WorkDepartment\Service\WorkDepartmentService;
use App\WorkEmployee\Service\WorkEmployeeDepartmentService;

/**
 * 部门人员-列表.
 *
 * Class ShowEmployeeLogic
 */
class ShowEmployeeLogic
{
    /**
     * @Inject
     * @var WorkEmployeeService
     */
    protected $workEmployeeService;

    /**
     * @Inject
     * @var WorkEmployeeDepartmentService
     */
    protected $workEmployeeDepartmentService;

    /**
     * @Inject
     * @var WorkDepartmentService
     */
    protected $workDepartmentService;

    /**
     * @var int
     */
    protected $corpId;

    /**
     * @param int $corp_id 登录用户企业ID信息
     * @param array $params 请求参数
     * @return array 响应数组
     */
    public function handle(int $corp_id, array $params): array
    {
        ## 处理请求参数
        $params = $this->handleParams($corp_id, $params);
        ## 查询数据
        return $this->getUserDepartment($params);
    }

    /**
     * 处理请求参数.
     *
     * @param int $corp_id 当前登录用户企业ID
     * @param array $params 请求参数
     * @return array 响应数组
     */
    private function handleParams(int $corp_id, array $params): array
    {
        ## 列表查询条件
        $this->corpId = $corp_id;
        $where['corp_id'] = $this->corpId;

        ## 根据部门id搜索
        if (! empty($params['departmentId'])) {
            ## 根据部门获取成员id
            $employeeIds = $this->getEmployeesByDepartmentId((int) $params['departmentId'],$corp_id);
            $where['id'] = $employeeIds;
        }

        ## 分页信息
        $options = [
            'page' => $params['page'],
            'perPage' => $params['perPage'],
            'orderByRaw' => 'update_time desc',
        ];

        return $data = [
            'where' => $where,
            'options' => $options,
        ];
    }

    /**
     * 获取成员列表.
     * @param array $params 请求参数
     * @return array 响应数组
     */
    private function getUserDepartment(array $params): array
    {
        ## 分页查询成员表数据表
        $columns = ['id', 'log_user_id', 'name', 'mobile'];
        $employeeList = $this->workEmployeeService->getWorkEmployeeList($params['where'], $columns, $params['options']);

        ## 组织响应数据
        $data = [
            'size' => $params['options']['perPage'],
            'total' => 0,
            'pages' => 0,
            'current' => 0,
            'records' => [],
        ];

        return empty($employeeList['data']) ? $data : $this->handleData($data, $employeeList);
    }

    /**
     * 组织响应数据.
     *
     * @param array $data 请求参数
     * @param array $employeeList ...
     * @return array 响应数组
     */
    private function handleData(array $data, array $employeeList): array
    {
        ## 分页数据
        $data['total'] = $employeeList['total'];
        $data['pages'] = $employeeList['last_page'];

        ## 数据处理
        $data['records'] = array_map(function ($val) {
            //$role = $this->getRoleNameByUserId($val['logUserId']);
            return [
                'employeeId' => $val['id'],
                'employeeName' => $val['name'],
                'phone' => $val['mobile'],
                'roleName' => ! empty($role) ? $role['name'] : '',
            ];
        }, $employeeList['data']);

        return $data;
    }


    /**
     * 根据部门id-获取成员.
     *
     * @param int $departmentId 部门id
     * @param int $corpId 企业id
     */
    private function getEmployeesByDepartmentId(int $departmentId,$corpId = ''): array
    {
        $columns = ['employee_id'];
        $employee = $this->workEmployeeDepartmentService->getWorkEmployeeDepartmentsByDepartmentIds([$departmentId], $columns);
        if(!empty($employee)) {
            $pdata = $this->workDepartmentService->getWorkDepartmentsByParentId($departmentId,['*'],$corpId);
            if(!empty($pdata)){
                $employeePush = $this->workEmployeeDepartmentService->getWorkEmployeeDepartmentsByDepartmentIds(array_column($pdata, 'id'), $columns);
                if(!empty($employeePush)) $employee = array_merge($employee,$employeePush);
            }
        }
        return ! empty($employee) ? array_column($employee, 'employeeId') : [];
    }
}
